#ifndef __ETH_NX_DRIVER_H
#define __ETH_NX_DRIVER_H

#include "stm32f4xx.h"
#include "nx_api.h"

/* Define Ethernet address format.  This is prepended to the incoming IP
   and ARP/RARP messages.  The frame beginning is 14 bytes, but for speed
   purposes, we are going to assume there are 16 bytes free in front of the
   prepend pointer and that the prepend pointer is 32-bit aligned.

    Byte Offset     Size            Meaning

        0           6           Destination Ethernet Address
        6           6           Source Ethernet Address
        12          2           Ethernet Frame Type, where:

                                        0x0800 -> IP Datagram
                                        0x0806 -> ARP Request/Reply
                                        0x0835 -> RARP request reply

        42          18          Padding on ARP and RARP messages only.  */
#define NX_DRIVER_ETHERNET_IP                   0x0800
#define NX_DRIVER_ETHERNET_IPV6                 0x86dd
#define NX_DRIVER_ETHERNET_ARP                  0x0806
#define NX_DRIVER_ETHERNET_RARP                 0x8035

/* Define the Link MTU. Note this is not the same as the IP MTU.  The Link MTU
   includes the addition of the Physical Network header (usually Ethernet). This
   should be larger than the IP instance MTU by the size of the physical header. */
#define NX_DRIVER_ETHERNET_MTU                  1514
/* D.MAC + S.MAC + type
	 6byte + 6byte + 2byte*/
#define NX_DRIVER_ETHERNET_FRAME_SIZE           14

/*用于延迟处理*/
#define NX_DRIVER_DEFERRED_PACKET_RECEIVED      1
#define NX_DRIVER_DEFERRED_DEVICE_RESET         2
#define NX_DRIVER_DEFERRED_PACKET_TRANSMITTED   4

/*用于驱动状态*/
#define NX_DRIVER_STATE_NOT_INITIALIZED         1
#define NX_DRIVER_STATE_INITIALIZE_FAILED       2
#define NX_DRIVER_STATE_INITIALIZED             3    
#define NX_DRIVER_STATE_LINK_ENABLED            4    

#define NX_DRIVER_ERROR                         90

/*移除以太帧包头*/
#define NX_DRIVER_ETHERNET_HEADER_REMOVE(p)   \
{   \
    p -> nx_packet_prepend_ptr =  p -> nx_packet_prepend_ptr + NX_DRIVER_ETHERNET_FRAME_SIZE;  \
    p -> nx_packet_length =  p -> nx_packet_length - NX_DRIVER_ETHERNET_FRAME_SIZE;    \
}

/****** DRIVER SPECIFIC ****** Start of part/vendor specific constants area.  Include any such constants here!  */
/* Define the number of descriptors and attached packets for transmit and receive operations. */
#ifndef NX_DRIVER_TX_DESCRIPTORS   
#define NX_DRIVER_TX_DESCRIPTORS   128
#endif

#ifndef NX_DRIVER_RX_DESCRIPTORS
#define NX_DRIVER_RX_DESCRIPTORS   16
#endif
/****** DRIVER SPECIFIC ****** End of part/vendor specific constant area!  */

/* Define basic Ethernet driver information typedef. Note that this typedefs is designed to be used only
   in the driver's C file. */

typedef struct NX_DRIVER_INFORMATION_STRUCT
{
	/* NetX IP instance that this driver is attached to.  */
	NX_IP               *nx_driver_information_ip_ptr;

	/* Driver's current state.  */
	ULONG               nx_driver_information_state ;

	/* Packet pool used for receiving packets. */
	NX_PACKET_POOL      *nx_driver_information_packet_pool_ptr;

	/* Define the driver interface association.  */
	NX_INTERFACE        *nx_driver_information_interface;

	/* Define the deferred event field. This will contain bits representing events 
		 deferred from the ISR for processing in the thread context.  */
	ULONG               nx_driver_information_deferred_events;

	/****** DRIVER SPECIFIC ****** Start of part/vendor specific driver information area.  Include any such constants here!  */

	/* Indices to current receive/transmit descriptors.  */
	UINT                nx_driver_information_receive_current_index;
	UINT                nx_driver_information_transmit_current_index;

	/* Transmit release index.  */
	UINT                nx_driver_information_transmit_release_index;

	/* Define the number of transmit buffers in use.  */
	UINT                nx_driver_information_number_of_transmit_buffers_in_use;

	/* Define the Ethernet RX & TX DMA Descriptors.  */
	ETH_DMADescTypeDef  nx_driver_information_dma_rx_descriptors[NX_DRIVER_RX_DESCRIPTORS];
	ETH_DMADescTypeDef  nx_driver_information_dma_tx_descriptors[NX_DRIVER_TX_DESCRIPTORS];

	/* Define the association between buffer descriptors and NetX packets.  */
	NX_PACKET           *nx_driver_information_transmit_packets[NX_DRIVER_TX_DESCRIPTORS];
	NX_PACKET           *nx_driver_information_receive_packets[NX_DRIVER_RX_DESCRIPTORS];

	/* Define the size of a rx buffer size.  */
	ULONG               nx_driver_information_rx_buffer_size;

	ULONG               nx_driver_information_multicast_count;

	/****** DRIVER SPECIFIC ****** End of part/vendor specific driver information area.  */

}	NX_DRIVER_INFORMATION;

VOID nx_driver_entry(NX_IP_DRIVER *driver_req_ptr);

#endif/*__ETH_NX_DRIVER_H*/
